/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apache.http.examples.client;

import db.MySQLDataManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.util.EntityUtils;


public class Test {

    private static  ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    
    private static HttpHost proxy = new HttpHost("110.139.175.172",8080,"http");
    private static DefaultHttpClient http = new DefaultHttpClient(cm);
    private static int CONNECTION_TIMEOUT = 20000;
    
    public void Test()
    {
        cm.setMaxTotal(100);
        http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    /* remove leading whitespace */
    public static String ltrim(String source) {
        return source.replaceAll("^\\s+", "");
    }

    public static String removeSpaces(String s) {
      StringTokenizer st = new StringTokenizer(s," ",false);
      String t="";
      while (st.hasMoreElements()) t += st.nextElement();
      return t;
    }


    public static void getUrl(String url, String Chareset,int i) throws IOException, ParseException
    {
        boolean check1 = false;
        boolean check1_other = false;
        boolean check2 = false;

        boolean check2_break_time1 = false;

        String quyen = null;
        int check_yes = 0;
        int check_yes_other = 0;
        int count = 0;
        String[] str = null;

        String row = "";
        String team1 = "";
        String team2 = "";

        String euro_1 = "";
        String euro_2 = "";
        String euro_3 = "";

        String asia_1 = "";
        String asia_2 = "";
        String asia_3 = "";

        String tx_1 = "";
        String tx_2 = "";
        String tx_3 = "";

        String hour = "";
        String day = "";
        
        Map m = new TreeMap();
        Map<Integer, ArrayList> bet_result = Collections.synchronizedMap(new HashMap<Integer, ArrayList>());
        List l = new ArrayList();
        List l_other = new ArrayList();

        HttpGet get = new HttpGet(url);//uri
        get.setHeader("Content-Type", "text/html");
        get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
        get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Accept-Language", "en-us,en;q=0.5");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Keep-Alive","300");
        get.setHeader("Cookie", "CFNAME1=975ecb719692fa2bc7255b0c2dd2f3a4");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Referer","Referer: https://login.findmespot.com/faces  /trackerunit.jsp");        
        get.getParams().setParameter("http.socket.timeout",new Integer(CONNECTION_TIMEOUT));//20000
        
        DefaultHttpClient http = new DefaultHttpClient(cm);//threads dont't share it
        //http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        String result = "";
        
        try {
            HttpResponse response = http.execute(get);
        if (response.getStatusLine().getStatusCode() != 200){//statusCode != HttpStatus.SC_OK) {
                System.err.println("HttpGet Method failed: "
                        + response.getStatusLine());//httpGet.getStatusLine()
        }                    
        InputStream in = response.getEntity().getContent();
        InputStreamReader is = new InputStreamReader(in);        
        BufferedReader br = new BufferedReader(is);
        String strTemp = br.readLine();
        while(strTemp != null) {
            //System.out.println(read);            
            strTemp =br.readLine();
            //System.err.println(read);
            strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                        //Debug.println(strTemp);

                        //---------------------------------Ti le aisan/euro------------------------------------------------------------------------------
                        if(check2_break_time1)
                        {
                            check2_break_time1 = false;
                            break;
                        }

                        if((strTemp.contains("size= 2 >")&&(strTemp.length() == 15))||((strTemp.contains("size= 2  >"))&&(strTemp.length() == 10)))
                        //if(strTemp.contains("size= 2  >"))
                        {
                            System.out.println(strTemp.length());
                            check1 = true;
                            check_yes ++;
                        }

                        if(check1)
                        {
                            l.add(strTemp);
                            if((strTemp.contains("size= 2 >")&&(l.size() > 1)&&(strTemp.length() == 15))||((strTemp.contains("size= 2  >"))&&(l.size() > 1)&&(strTemp.length() == 10)))
                            {                                
                                int countTemp = 0;
                                int sttEuro = 0;
                                for(int intIndex = 0; intIndex < l.size(); intIndex++)
                                {
                                    row = "";
                                    row = ltrim((String) l.get(intIndex));
                                    //Debug.println(row);

                                    if(row.contains("Latest Over/Under (Total Goals) Odds"))
                                    {
                                        check2_break_time1 = true;
                                        break;
                                    }

                                    if(row.contains(",'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                    {
                                      
                                      String[] arr_team = row.substring(0, row.indexOf(",'top")).replaceAll("'", "").split("vs");
                                      
                                      team1 = arr_team[0];
                                      if(team1.contains("(N)"))
                                      {
                                            team1 = team1.replaceAll("(N)", "");
                                      }
                                      team2 = ltrim(arr_team[1].replaceAll("'", ""));
                                      if(team2.contains("(N)"))
                                      {
                                            team2 = team2.replaceAll("(N)", "");
                                      }
                                    }

                                    //get aisa_1
                                    if(row.contains("align=center nowrap><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_down.gif  width= 9  height= 9  align= middle ><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_up.gif  width= 9  height= 9  align= middle ><b>"))
                                    {
                                        if(row.length() < 48)
                                        {
                                            if(row.startsWith("src= http://imagescdn.asianbookie.com/arrow"))
                                            {
                                               String asia_1_temp = row.substring(row.indexOf("<b>"));
                                               asia_1 = asia_1_temp.replaceAll("<b>", "");
                                               //Debug.println("asia_1_1:"+asia_1);
                                            }
                                            else
                                            {                                                
                                                asia_1 = row.substring(row.length()-8, row.length()).replaceAll("<", "").replaceAll(">", "").replaceAll("b", "");
                                                //Debug.println("asia_1_2:"+asia_1);
                                            }
                                        }
                                    }

                                    //get asia_2
                                    if(row.contains("</b></font></td>"))
                                    {                                        
                                        String asia_2_temp = row.replaceAll("</b></font></td>", "");
                                        String asia_3_temp = asia_2_temp.substring(asia_2_temp.length() - 10, asia_2_temp.length());
                                        asia_2 = asia_3_temp.substring(asia_3_temp.indexOf(">")).replaceAll(">","");
                                        //Debug.println("asia_2:"+asia_2);
                                    }

                                    //get asia_3
                                    if(row.contains("</b></td>") || (row.contains("</b><img")))
                                    {
                                        
                                        if(row.contains("</b></td>"))
                                        {
                                            String asia_3_temp = row.replaceAll("</b></td>", "");
                                            asia_3 = asia_3_temp.substring(asia_3_temp.indexOf("<b>")).replaceAll("<b>","");
                                            //Debug.println("asia_3_1:"+asia_3);
                                        } else{
                                            asia_3 = row.replaceAll("</b><img", "").replaceAll("align= center  nowrap><b>", "");
                                            //Debug.println("asia_3_2:"+asia_3);
                                        }
                                    }

                                    //get euro 1
                                   if(row.contains("<font") && row.contains("</font>")&&row.contains("size")&& !row.contains("face")&& !row.contains("sup"))
                                    {
                                       
                                        String eur0_temp = row.replaceAll("<b>", "").replaceAll("</b>", "").replaceAll("</font>", "");
                                       
                                        if(sttEuro == 0)
                                        {
                                            euro_1 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                            //Debug.println("euro_1:"+euro_1);
                                        } else if(sttEuro == 1)
                                        {
                                            euro_2 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                             //Debug.println("euro_2:"+euro_2);
                                        } else if(sttEuro == 2)
                                        {
                                            euro_3 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                             //Debug.println("euro_3:"+euro_3);
                                        }
                                        sttEuro ++;
                                    //get euro 2
                                                                           
                                    }

                                    if(row.contains("&nbsp;&nbsp;") && row.contains("</font></td>"))
                                    {                                        
                                         String time_temp = row.replaceAll("</font></td>", "");
                                         String[] time = time_temp.split("&nbsp;&nbsp;");
                                         String[] arrHour = new String[2];
                                         hour   = time[1];
                                         //arrHour = hour.split(":");
                                         //hour = String.valueOf(Integer.valueOf(arrHour[0])-1)+":"+ arrHour[1];
                                         day    = time[0];
                                    }
                                }
                                Debug.println("ngon");
                                MySQLDataManager datamanager = new MySQLDataManager();
                                if(datamanager.checkExistAsianAndEuroRate(team1, team2, hour, day))
                                {
                                    datamanager.updateAsianAndEuroRate(team1, team2, euro_1, euro_2, euro_3, asia_1, asia_2, asia_3, "", "", "", hour, day);
                                } else {
                                    //check Double rate
                                    // check trung trong thoi gian 1 ngay
                                    if(!datamanager.checkExistDoubleAsianAndEuroRate(team1, team2, hour, day))
                                    {
                                        datamanager.insertResultInDB(team1, team2, euro_1, euro_2, euro_3, asia_1, asia_2, asia_3,"","","", hour, day, String.valueOf(i));
                                    }
                                }
                                l.clear();
                             // tinh not keo cuoi
                            }else if(l.toString().contains("</html>")){

                                int countTemp = 0;
                                int sttEuro = 0;
                                for(int intIndex = 0; intIndex < l.size(); intIndex++)
                                {

                                 try{
                                    row = "";
                                    row = ltrim((String) l.get(intIndex));
                                    Debug.println(row);
                                    
                                    if(row.contains("Latest Over/Under (Total Goals) Odds"))
                                    {
                                        check2_break_time1 = true;
                                        break;
                                    }
                                    
                                    if(row.contains(",'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                    {

                                      String[] arr_team = row.substring(0, row.indexOf(",'top")).replaceAll("'", "").split("vs");

                                      team1 = arr_team[0];
                                      if(team1.contains("(N)"))
                                      {
                                            team1 = team1.replaceAll("(N)", "");
                                      }
                                      team2 = ltrim(arr_team[1].replaceAll("'", ""));
                                      if(team2.contains("(N)"))
                                      {
                                            team2 = team2.replaceAll("(N)", "");
                                      }
                                    }

                                    //get aisa_1
                                    if(row.contains("align=center nowrap><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_down.gif  width= 9  height= 9  align= middle ><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_up.gif  width= 9  height= 9  align= middle ><b>"))
                                    {
                                        if(row.length() < 48)
                                        {
                                            if(row.startsWith("src= http://imagescdn.asianbookie.com/arrow"))
                                            {
                                               String asia_1_temp = row.substring(row.indexOf("<b>"));
                                               asia_1 = asia_1_temp.replaceAll("<b>", "");
                                               //Debug.println("asia_1_1:"+asia_1);
                                            }
                                            else
                                            {
                                                asia_1 = row.substring(row.length()-8, row.length()).replaceAll("<", "").replaceAll(">", "").replaceAll("b", "");
                                                //Debug.println("asia_1_2:"+asia_1);
                                            }
                                        }
                                    }

                                    //get asia_2
                                    if(row.contains("</b></font></td>"))
                                    {
                                        String asia_2_temp = row.replaceAll("</b></font></td>", "");
                                        String asia_3_temp = asia_2_temp.substring(asia_2_temp.length() - 10, asia_2_temp.length());
                                        asia_2 = asia_3_temp.substring(asia_3_temp.indexOf(">")).replaceAll(">","");
                                        //Debug.println("asia_2:"+asia_2);
                                    }

                                    //get asia_3
                                    if((row.contains("</b></td>") || (row.contains("</b><img")))&&(!row.contains("</font>")))
                                    {

                                        if(row.contains("</b></td>"))
                                        {
                                            String asia_3_temp = row.replaceAll("</b></td>", "");
                                            asia_3 = asia_3_temp.substring(asia_3_temp.indexOf("<b>")).replaceAll("<b>","");
                                            //Debug.println("asia_3_1:"+asia_3);
                                        } else{
                                            asia_3 = row.replaceAll("</b><img", "").replaceAll("align= center  nowrap><b>", "");
                                            //Debug.println("asia_3_2:"+asia_3);
                                        }
                                    }

                                    //get euro 1
                                   if(row.contains("<font") && row.contains("</font>")&&row.contains("size")&& !row.contains("face")&& !row.contains("sup"))
                                    {

                                        String eur0_temp = row.replaceAll("<b>", "").replaceAll("</b>", "").replaceAll("</font>", "");

                                        if(sttEuro == 0)
                                        {
                                            euro_1 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                            //Debug.println("euro_1:"+euro_1);
                                        } else if(sttEuro == 1)
                                        {
                                            euro_2 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                             //Debug.println("euro_2:"+euro_2);
                                        } else if(sttEuro == 2)
                                        {
                                            euro_3 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                             //Debug.println("euro_3:"+euro_3);
                                        }
                                        sttEuro ++;
                                    //get euro 2

                                    }

                                    if(row.contains("&nbsp;&nbsp;") && row.contains("</font></td>"))
                                    {
                                         String time_temp = row.replaceAll("</font></td>", "");
                                         String[] time = time_temp.split("&nbsp;&nbsp;");
                                         String[] arrHour = new String[2];
                                         hour   = time[1];
                                         //arrHour = hour.split(":");
                                         //hour = String.valueOf(Integer.valueOf(arrHour[0])-1)+":"+ arrHour[1];
                                         day    = time[0];
                                    }
                                   }catch (Exception ex){
                                        System.out.println("exception:"+ex.getMessage());
                                   }

                                }
                                Debug.println("ngon");
                                MySQLDataManager datamanager = new MySQLDataManager();
                                if(datamanager.checkExistAsianAndEuroRate(team1, team2, hour, day))
                                {
                                    datamanager.updateAsianAndEuroRate(team1, team2, euro_1, euro_2, euro_3, asia_1, asia_2, asia_3, "", "", "", hour, day);
                                } else {
                                    //check Double rate
                                    // check trung trong thoi gian 1 ngay
                                    if(!datamanager.checkExistDoubleAsianAndEuroRate(team1, team2, hour, day))
                                    {
                                        datamanager.insertResultInDB(team1, team2, euro_1, euro_2, euro_3, asia_1, asia_2, asia_3,"","","", hour, day, String.valueOf(i));
                                    }
                                }
                                l.clear();
                                
                            }


                        }
        }       
    } catch(java.net.SocketException ee)
    {
            ee.printStackTrace();
            //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ee);
    }
        catch (IOException e) {
            //throw new Exception(e);
            //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, e);//TODO Debug
    } finally {
        get.abort();//releaseConnection();//TODO http.getConnectionManager().shutdown();?
        get = null;
                http = null;//clean almost all the resources
    }       
    }
    public static void getUrlUpdateRate(String url, String Chareset,int i) throws IOException, ParseException
    {
        boolean check1 = false;
        boolean check1_other = false;
        boolean check2 = false;

        boolean check2_break_time1 = false;

        String quyen = null;
        int check_yes = 0;
        int check_yes_other = 0;
        int count = 0;
        String[] str = null;

        String row = "";
        String team1 = "";
        String team2 = "";

        String euro_1 = "";
        String euro_2 = "";
        String euro_3 = "";

        String asia_1 = "";
        String asia_2 = "";
        String asia_3 = "";

        String tx_1 = "";
        String tx_2 = "";
        String tx_3 = "";

        String hour = "";
        String day = "";
        //MySQLDataManager datamanager = new MySQLDataManager();
        Map m = new TreeMap();
        Map<Integer, ArrayList> bet_result = Collections.synchronizedMap(new HashMap<Integer, ArrayList>());
        List l = new ArrayList();
        List l_other = new ArrayList();

        HttpGet get = new HttpGet(url);//uri
        get.setHeader("Content-Type", "text/html");
        get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
        get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Accept-Language", "en-us,en;q=0.5");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Keep-Alive","300");
        get.setHeader("Cookie", "CFNAME1=975ecb719692fa2bc7255b0c2dd2f3a4");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Referer","Referer: https://login.findmespot.com/faces  /trackerunit.jsp");
        get.getParams().setParameter("http.socket.timeout",new Integer(CONNECTION_TIMEOUT));//20000

        DefaultHttpClient http = new DefaultHttpClient(cm);//threads dont't share it
        //http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        String result = "";

        try {
            HttpResponse response = http.execute(get);
        if (response.getStatusLine().getStatusCode() != 200){//statusCode != HttpStatus.SC_OK) {
                System.err.println("HttpGet Method failed: "
                        + response.getStatusLine());//httpGet.getStatusLine()
        }
        InputStream in = response.getEntity().getContent();
        InputStreamReader is = new InputStreamReader(in);
        StringBuilder sb=new StringBuilder();
        BufferedReader br = new BufferedReader(is);
        String strTemp = br.readLine();
        while(strTemp != null) {
            //System.out.println(read);
            sb.append(strTemp);
            strTemp =br.readLine();
             strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                //neu den dong chua chuoi nay thi moi bat dau tinh
                if(strTemp.contains("Over/Under (Total Goals)")) {
                    check2_break_time1 = true;
                }
             
                if(check2_break_time1)
                {
                            //---------------------------------Ti le tai/xiu------------------------------------------------------------------------------
                            //get module
                            //Debug.println(strTemp);
                            //bat dau mot keo moi
                            if((strTemp.startsWith("<font size= 2  >")&&(strTemp.length() == 16)) || (strTemp.startsWith("<font size= 2 >")&&(strTemp.length() == 15)))
                            //if(strTemp.startsWith("<font size= 2 >"))
                            {
                                //System.out.println(strTemp.length());
                                check1_other = true;
                                //check_yes_other ++;
                            }
                            if(check1_other)
                            {
                                //de lay tung keo mot
                                l_other.add(strTemp);

                                if((strTemp.startsWith("<font size= 2  >")&&(l_other.size() > 1)&&(strTemp.length() == 16))|| (strTemp.startsWith("<font size= 2 >")&&(l_other.size() > 1)&&(strTemp.length() == 15)))
                                {
                                    //bet_result.put(check_yes, (ArrayList) l);
                                    //ArrayList l_temp = new ArrayList();
                                    int countTemp1 = 0;
                                    int countTemp2 = 0;
                                    String hour_tx = "";
                                    String date_tx = "";
                                    try{
                                        for(int intIndex = 0; intIndex < l_other.size(); intIndex++)
                                        {
                                            String row1 = ltrim((String) l_other.get(intIndex));

                                            if(row1.contains(",'top=100px,left=250px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                            {
                                              //String teamTemp = row1.replaceAll("'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no'", "");
                                              String[] arr_team = row1.substring(0, row1.indexOf(",'top")).replaceAll("'", "").replaceAll(" (N)", "").split(" vs ");
                                              team1 = arr_team[0];
                                              team2 = arr_team[1];
                                            }

                                            if(row1.contains("<b>")&& row1.startsWith("<b>")&& row1.length() <10)
                                            {
                                                countTemp1 ++;
                                                if(countTemp1 == 1)
                                                {
                                                    tx_1 = row1.replaceAll("<b>","");
                                                }
                                                else{
                                                    continue;
                                                }
                                            }

                                            if(row1.contains("&nbsp;&nbsp;") && row1.contains("</font></td>"))
                                            {
                                                String temp_str = row1.replaceAll("</font></td>", "");
                                                String[] arr_temp = new String[3];
                                                arr_temp = temp_str.split("&nbsp;&nbsp;");
                                                hour_tx = arr_temp[1];
                                                String[] date_tx1 = new String[10];
                                                date_tx1 = arr_temp[0].split("/");
                                                String temp1 = TimeUtil.convertMonthToInt(Integer.valueOf(date_tx1[1]));
                                                date_tx = date_tx1[0]+"/"+String.valueOf(temp1);
                                            }

                                            if(row1.contains("</b></font></td>"))
                                            {
                                                String tx_temp = row1.replaceAll("</b></font></td>", "");
                                                String tx_temp_1 = tx_temp.substring(tx_temp.length()-8, tx_temp.length());
                                                tx_2 = tx_temp_1.substring(tx_temp_1.indexOf(">"), tx_temp_1.length()).replaceAll(">", "");
                                            }

                                            if(row1.contains("</b> </td>"))
                                            {
                                                countTemp2 ++;
                                                if(countTemp2 == 2)
                                                {
                                                    tx_3 = row1.replaceAll("</b> </td>","");
                                                }else{
                                                    continue;
                                                }
                                            }
                                        }
                                    } catch (Exception ex){
                                        System.out.println("exception:"+ex.getMessage());
                                    }
                                    
                                    Debug.println("ngon");
                                    MySQLDataManager datamanager = new MySQLDataManager();

                                    if(datamanager.checkExistAsianAndEuroRate(team1, team2, hour_tx, date_tx)){
                                        datamanager.updateTX(team1, team2, tx_1, tx_2, tx_3,hour_tx,date_tx);
                                    }
                                    check1_other = true;
                                    l_other.clear();
                                 //lay keo cuoi 
                                } else if(l_other.toString().contains("</html>")){                                                                            
                                    int countTemp1 = 0;
                                    int countTemp2 = 0;
                                    String hour_tx = "";
                                    String date_tx = "";
                                    try{
                                        for(int intIndex = 0; intIndex < l_other.size(); intIndex++)
                                        {
                                            String row1 = ltrim((String) l_other.get(intIndex));

                                            if(row1.contains(",'top=100px,left=250px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                            {
                                              //String teamTemp = row1.replaceAll("'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no'", "");
                                              String[] arr_team = row1.substring(0, row1.indexOf(",'top")).replaceAll("'", "").replaceAll(" (N)", "").split(" vs ");
                                              team1 = arr_team[0];
                                              team2 = arr_team[1];
                                            }

                                            if(row1.contains("<b>")&& row1.startsWith("<b>")&& row1.length() <10)
                                            {
                                                countTemp1 ++;
                                                if(countTemp1 == 1)
                                                {
                                                    tx_1 = row1.replaceAll("<b>","");
                                                }
                                                else{
                                                    continue;
                                                }
                                            }

                                            if(row1.contains("&nbsp;&nbsp;") && row1.contains("</font></td>"))
                                            {
                                                String temp_str = row1.replaceAll("</font></td>", "");
                                                String[] arr_temp = new String[3];
                                                arr_temp = temp_str.split("&nbsp;&nbsp;");
                                                hour_tx = arr_temp[1];
                                                String[] date_tx1 = new String[10];
                                                date_tx1 = arr_temp[0].split("/");
                                                String temp1 = TimeUtil.convertMonthToInt(Integer.valueOf(date_tx1[1]));
                                                date_tx = date_tx1[0]+"/"+String.valueOf(temp1);
                                            }

                                            if(row1.contains("</b></font></td>"))
                                            {
                                                String tx_temp = row1.replaceAll("</b></font></td>", "");
                                                String tx_temp_1 = tx_temp.substring(tx_temp.length()-8, tx_temp.length());
                                                tx_2 = tx_temp_1.substring(tx_temp_1.indexOf(">"), tx_temp_1.length()).replaceAll(">", "");
                                            }

                                            if(row1.contains("</b> </td>"))
                                            {
                                                countTemp2 ++;
                                                if(countTemp2 == 2)
                                                {
                                                    tx_3 = row1.replaceAll("</b> </td>","");
                                                }else{
                                                    continue;
                                                }
                                            }
                                        }
                                    } catch (Exception ex){
                                        System.out.println("exception:"+ex.getMessage());
                                    }

                                    Debug.println("ngon");
                                    MySQLDataManager datamanager = new MySQLDataManager();

                                    if(datamanager.checkExistAsianAndEuroRate(team1, team2, hour_tx, date_tx)){
                                        datamanager.updateTX(team1, team2, tx_1, tx_2, tx_3,hour_tx,date_tx);
                                    }
                                    check1_other = true;
                                    l_other.clear();
                                }

                            }
                }
            }
        }catch (java.net.SocketException ee)
        {
            ee.printStackTrace();
                //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ee);
        }
        catch (IOException e) {
                //throw new Exception(e);
                //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, e);//TODO Debug
        } 
    }

    public static void getBXH(String url, String Chareset,String year) throws IOException, ParseException
    {
        boolean check1 = false;
                             
        List l = new ArrayList();        

        HttpGet get = new HttpGet(url);//uri
        get.setHeader("Content-Type", "text/html");
        get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
        get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Accept-Language", "en-us,en;q=0.5");//"utf-8;q=0.7,*;q=0.7");
        get.setHeader("Keep-Alive","300");
        get.setHeader("Cookie", "CFNAME1=975ecb719692fa2bc7255b0c2dd2f3a4");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Referer","Referer: https://login.findmespot.com/faces  /trackerunit.jsp");
        get.getParams().setParameter("http.socket.timeout",new Integer(CONNECTION_TIMEOUT));//20000

        DefaultHttpClient http = new DefaultHttpClient(cm);//threads dont't share it
        //http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        StringBuilder sb = new StringBuilder();

        try {
            HttpResponse response = http.execute(get);
        if (response.getStatusLine().getStatusCode() != 200){//statusCode != HttpStatus.SC_OK) {
                System.err.println("HttpGet Method failed: "
                        + response.getStatusLine());//httpGet.getStatusLine()
        }
        
        InputStream in = response.getEntity().getContent();
        InputStreamReader is = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(is);
        String strTemp = br.readLine();
        while(strTemp != null) {
            //System.out.println(read);
            strTemp =br.readLine();
            //System.err.println(read);
            strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                        //Debug.println(strTemp);
                        //---------------------------------Bang xep hang------------------------------------------------------------------------------                                            
                        if(strTemp.startsWith("<tr style='background"))
                        {
                            System.out.println(strTemp.length());
                            check1 = true;                            
                        }

                        if(check1)
                        {
                            l.add(strTemp);                            
                            if(strTemp.contains("<tr style='background"))
                            {
                                String sotran = "";
                                String thang = "";
                                String hoa = "";

                                String thua = "";
                                String hieuso = "";
                                String diem = "";
                                int counter =0;
                                String team = "";
                                String stt = "";
                                try{                                
                                    for(int intIndex = 0; intIndex < l.size(); intIndex++)
                                    {
                                        String row1 = ltrim((String) l.get(intIndex));
                                            
                                        if(row1.contains("style= font-weight:bold; color:Blue; font-size:11px;"))
                                        {                                          
                                          String strTemp1 = row1.replaceAll("</a>", "");
                                          team = strTemp1.substring(strTemp1.indexOf(">"), strTemp1.length()).replaceAll(">", "");                                         
                                        }
                                       
                                        if(row1.contains("<td align= center  style= font-size:11px; border-color:#ccc; >"))
                                        {
                                            counter ++;
                                            if(counter == 1){
                                                stt = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(sotran);
                                            } else if(counter == 2){
                                                sotran = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(thang);
                                            }else if(counter == 3){
                                                thang = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(hoa);
                                            }else if(counter == 4){
                                                hoa = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(thua);
                                            }else if(counter == 5){
                                                thua = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(hieuso);
                                            }else if(counter == 6){
                                                hieuso = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(diem);
                                            }
                                            else if(counter == 7){
                                                diem = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(diem);
                                            }
                                        }
                                    }
                                    
                                } catch (Exception ex){
                                    System.out.println("exception:"+ex.getMessage());
                                }
                                String result2 = stt+":"+team+":"+sotran +":"+thang+":"+hoa+":"+thua+":"+hieuso+":"+diem;
                                sb.append(result2).append(";");                                
                                l.clear();
                             // tinh not phat cuoi
                            }else if(l.toString().contains("</html>")){
                                   String sotran = "";
                                String thang = "";
                                String hoa = "";

                                String thua = "";
                                String hieuso = "";
                                String diem = "";
                                int counter =0;
                                String team = "";
                                String stt = "";
                                try{
                                    for(int intIndex = 0; intIndex < l.size(); intIndex++)
                                    {
                                        String row1 = ltrim((String) l.get(intIndex));

                                        if(row1.contains("style= font-weight:bold; color:Blue; font-size:11px;"))
                                        {
                                          //String teamTemp = row1.replaceAll("'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no'", "");
                                          String strTemp1 = row1.replaceAll("</a>", "");
                                          team = strTemp1.substring(strTemp1.indexOf(">"), strTemp1.length()).replaceAll(">", "");
                                         // Debug.println(team);
                                        }

                                        if(row1.contains("<td align= center  style= font-size:11px; border-color:#ccc; >"))
                                        {
                                            counter ++;
                                            if(counter == 1){
                                                stt = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(sotran);
                                            } else if(counter == 2){
                                                sotran = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(thang);
                                            }else if(counter == 3){
                                                thang = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                              //  Debug.println(hoa);
                                            }else if(counter == 4){
                                                hoa = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(thua);
                                            }else if(counter == 5){
                                                thua = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(hieuso);
                                            }else if(counter == 6){
                                                hieuso = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(diem);
                                            }
                                            else if(counter == 7){
                                                diem = row1.replaceAll("<td align= center  style= font-size:11px; border-color:#ccc; >","").replaceAll("</td>", "");
                                               // Debug.println(diem);
                                            }
                                        }                                     
                                    }

                                } catch (Exception ex){
                                    System.out.println("exception:"+ex.getMessage());
                                }
                                String result2 = stt+":"+team+":"+sotran +":"+thang+":"+hoa+":"+thua+":"+hieuso+":"+diem;
                                sb.append(result2);
                                MySQLDataManager datamanager = new MySQLDataManager();
                                /*
                                if(datamanager.checkExistAsianAndEuroRate(team1, team2, hour_tx, date_tx)){
                                        datamanager.updateTX(team1, team2, tx_1, tx_2, tx_3,hour_tx,date_tx);
                                }*/
                                l.clear();
                            }
                        }
        }
    } catch(java.net.SocketException ee)
    {
            ee.printStackTrace();
            //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ee);
    }
        catch (IOException e) {
            //throw new Exception(e);
            //Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, e);//TODO Debug
    } finally {
        get.abort();//releaseConnection();//TODO http.getConnectionManager().shutdown();?
        get = null;
                http = null;//clean almost all the resources
    }
    }
}
